package com.duowei.diandan.fragment.dialog;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.DeskStatus;
import com.duowei.diandan.databinding.MergeTableDialogFragmentBinding;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MergeTableDialogFragment extends BaseDialogFragment {

    private MergeTableDialogFragmentBinding mBinding;

    private List<DeskStatus> mList;

    public static MergeTableDialogFragment newInstance(ArrayList<? extends Parcelable> list) {

        Bundle args = new Bundle();
        args.putParcelableArrayList("list", list);

        MergeTableDialogFragment fragment = new MergeTableDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mList = getArguments().getParcelableArrayList("list");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_merge_table_dialog, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        mBinding.listView.setAdapter(new CustomAdapter(getContext(), mList));
        mBinding.listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DeskStatus deskStatus = mList.get(position);
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(deskStatus);
                }
                dismiss();
            }
        });

        mBinding.cancelButton.setOnClickListener(v -> dismiss());
    }

    private static class CustomAdapter extends ArrayAdapter<DeskStatus> {

        CustomAdapter(@NonNull Context context, @NonNull List<DeskStatus> objects) {
            super(context, android.R.layout.simple_list_item_1, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_merge_table, parent, false);
                holder = new ViewHolder();
                holder.text1 = convertView.findViewById(R.id.text1);
                holder.text2 = convertView.findViewById(R.id.text2);
                holder.text3 = convertView.findViewById(R.id.text3);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            DeskStatus ds = getItem(position);
            //noinspection ConstantConditions
            holder.text1.setText(ds.getWmdbh());
            holder.text2.setText(String.format(Locale.CHINA, "¥%.2f", ds.getYs()));
            holder.text3.setText(ds.getJcsj().toLocalTime().toString("HH:mm:ss"));

            return convertView;
        }

        static class ViewHolder {
            TextView text1;
            TextView text2;
            TextView text3;
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(DeskStatus deskStatus);
    }
}
